using Godot;
using System;

public partial class MainScene : Node2D
{
    private RectCoordinate _rectCoordinate;
    private HSlider _slider;

    private Vector2 _dragStartPoint;

    private bool _leftButtonPressedState;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        _slider = GetNode<HSlider>(@"ResizeSlider");
        _rectCoordinate = GetNode<RectCoordinate>(@"RectCoordinate");

        _slider.ValueChanged += _slider_ValueChanged;
        _slider.Value = _rectCoordinate.Interval;
    }

    private void _slider_ValueChanged(double value)
    {
        _rectCoordinate.Interval = (float)value;
        _rectCoordinate.QueueRedraw();
    }

    // Called every frame. 'delta' is the elapsed time since the previous frame.
    public override void _Process(double delta)
    {
        var currentLeftButtonState = Input.IsMouseButtonPressed(MouseButton.Left);
        if (currentLeftButtonState == true && _leftButtonPressedState == false)
        {
            OnMousePressed();
        }
        else if (currentLeftButtonState == false && _leftButtonPressedState == true)
        {
            OnMouseReleased();
        }
        else if (currentLeftButtonState == true)
        {
            OnMouseMove();
        }



        _leftButtonPressedState = currentLeftButtonState;
    }

    private void OnMousePressed()
    {
        var viewport = GetViewport();
        var mpos = viewport.GetMousePosition();
        var opos = _rectCoordinate.Position;
        _dragStartPoint = mpos - opos;
    }

    private void OnMouseReleased()
    {

    }

    private void OnMouseMove()
    {
        var viewport = GetViewport();
        var mpos = viewport.GetMousePosition();
        _rectCoordinate.Position = mpos - _dragStartPoint;
        _rectCoordinate.QueueRedraw();

    }


}
